<?php


namespace app\api\controller;


use app\api\model\Bind;
use app\api\model\Member;
use think\Request;

//http://cosmo.com/api/msg/getMsg

class Msg extends BaseApi {

    protected $bindItem;

    public function __construct(Request $request = null){
        parent::__construct($request);
        $bind = new Bind();
        $this->bindItem = $bind->field('binds')->find();
    }

    public function getMsgList(){
        if (!$this->request->param('openid') ||!$this->request->param('page')) {
            return json(['code' => -1, 'msg' => '缺少参数']);
        }

        $openid = trim($this->request->param('openid'));
        $page = max(1,intval($this->request->param('page')));
        $pagesize = intval($this->request->param('pagesize'));
        $msg = new \app\api\model\Msg();
        $msgList = $msg->getList($openid,$page,$pagesize);

        if(!$msgList->isEmpty()){
            foreach ($msgList as $msg){
                $member = new Member();
                $msg->offname = $member->getMember($msg->offid)->nickname;
                $msg->bindtime = $msg->bindtime + $this->bindItem->binds * 24 * 60 * 60;
            }
        }
        return json(['code'=>1,'data'=>$msgList]);
    }


    public function getMsg(){
        if (!$this->request->param('msgid')) {
            return json(['code' => -1, 'msg' => '缺少参数']);
        }

        $msgid = intval($this->request->param('msgid'));

        $msg = new \app\api\model\Msg();
        $msgItem = $msg->where('id',$msgid)->find();

        if(empty($msgItem)){
            return json(['code' => -1, 'msg' => '该消息不存在或已删除']);
        }

        $member = new Member();
        $msgItem->offname = $member->getMember($msgItem->offid)->nickname;
        $msgItem->bindtime = $msgItem->bindtime + $this->bindItem->binds * 24 * 60 * 60;

        return json(['code' => 1, 'data' => $msgItem]);

    }



}